
'use strict';
const app = require("tcb-admin-node");
function times33(str){
    for(var i = 0, len = str.length,hash = 5381; i < len; ++i){
       hash += (hash << 5) + str.charCodeAt(i);
    };
    return hash & 0x7fffffff;
}
exports.main = async (event, context, callback) => {
    app.init({env: process.env.ENV})
    const db = app.database();
    const _ = db.command;
	let DB = process.env.DB
	let callbackfunciton = "callback"
	
    var accessHash = times33("wu"+process.env.accessKey).toString(36)+times33("han"+process.env.accessKey).toString(36);
    var accessHash2 = times33("wu"+process.env.accessKey2).toString(36)+times33("han"+process.env.accessKey2).toString(36);
    console.log(accessHash2)
    var collection;
    if(event.path && (event.path.indexOf(accessHash)>-1 || event.path.indexOf(accessHash2)>-1)){
		DB = process.env.CASE_DB
		callbackfunciton = "detailCallback"
        collection=db.collection(DB);
	}else{
		DB = process.env.CASE_DB
		callbackfunciton = "caseCallback"
        collection = db.collection(DB).field({ 'lng': true,'lat': true,"types":true })
	}
	let limit = 999,skip=0;
	let all = [],t=new Array(limit);
    for(;(t.length == limit);skip+=limit){
		t=(await collection.skip(skip).limit(limit).get()).data;
		all = all.concat(t);
	}
    for(var i=0;i<all.length;i++){
        delete all[i]._id
    }
    return {
            'isBase64Encoded': false,
            'statusCode' : 200,
            'headers' : {'Content-Type':'application/x-javascript; charset=utf-8'},
            'body' : callbackfunciton+"("+JSON.stringify(all)+")"
        };
};
